#region Copyright

/*---------------------------------------------------------------------------
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under 
 * the License.
 * 
 * The Initial Developer of the Original Code is Robert Smyth.
 * Portions created by Robert Smyth are Copyright (C) 2008.
 * 
 * All Rights Reserved.
 *---------------------------------------------------------------------------*/

#endregion Copyright

using System.Collections.Generic;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Editor;
using JetBrains.ReSharper.Psi;
using JetBrains.ActionManagement;
using JetBrains.ReSharper.EditorManager;
using JetBrains.UI;
using NoeticTools.SharpProbe.UI;
using DataConstants=JetBrains.ReSharper.DataConstants;


namespace NoeticTools.SharpProbe
{
    [ActionHandler("AddMenuItem.SharpProbeAction")]
    public class SharpProbeAction : IActionHandler
    {
        public bool Update(IDataContext context, ActionPresentation presentation, DelegateUpdate nextUpdate)
        {
            // fetch active solution from context
            ISolution solution = context.GetData(DataConstants.SOLUTION);

            // enable this action if there is an active solution, disable otherwise
            return solution != null;
        }

        public void Execute(IDataContext context, DelegateExecute nextExecute)
        {
            List<MemberValue> memberValues = new List<MemberValue>();

            ISolution solution = context.GetData(DataConstants.SOLUTION);
            memberValues.Add(new MemberValue("SOLUTION Name", solution.Name));
            memberValues.Add(new MemberValue("SOLUTION SolutionProject.Name", solution.SolutionProject.Name));

            ContextLocation contextLocation = context.GetData(DataConstants.CONTEXT_SCREEN_LOCATION);
            string contextLocationXY = contextLocation == null ? "null" : string.Format("{0}, {1}", contextLocation.X, contextLocation.Y);
            memberValues.Add(new MemberValue("CONTEXT_SCREEN_LOCATION X,Y", contextLocationXY));

            DocumentOffset documentOffset = context.GetData(DataConstants.DOCUMENT_OFFSET);
            string documentOffsetValue = documentOffset == null ? "null" : documentOffset.Value.ToString();
            memberValues.Add(new MemberValue("DOCUMENT_OFFSET Value", documentOffsetValue));

            IProjectModelElement pme = context.GetData(DataConstants.PROJECT_MODEL_ELEMENT);
            string projectModelElement = pme == null ? "null" : pme.Name;
            memberValues.Add(new MemberValue("PROJECT_MODEL_ELEMENT Name", projectModelElement));

            IDeclaredElement de = context.GetData(DataConstants.DECLARED_ELEMENT);
            string declaredElement = de == null ? "null" : de.ShortName;
            memberValues.Add(new MemberValue("DECLARED_ELEMENT ShortName", declaredElement));

            IDeclaredElement tote = context.GetData(DataConstants.TYPE_OR_TYPE_MEMBER);
            string typeMember = tote == null ? "null" : tote.ShortName;
            memberValues.Add(new MemberValue("TYPE_OR_TYPE_MEMBER ShortName", typeMember));

            IEditor ed = context.GetData(DataConstants.EDITOR);
            string editor = ed == null ? "null" : ed.Text;
            memberValues.Add(new MemberValue("EDITOR Text", editor));

            DumpView view = new DumpView(memberValues.ToArray());
            view.Show();
        }
    }
}